package net.hvacapp.cmvrfforandroid.ui.project;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.mirror.android.common.util.StringUtils;

import net.hvacapp.cmvrfforandroid.R;
import net.hvacapp.cmvrfforandroid.http.BasicHttpClient;
import net.hvacapp.cmvrfforandroid.http.HttpCallbackHandle;
import net.hvacapp.cmvrfforandroid.http.HttpConstant;
import net.hvacapp.cmvrfforandroid.manager.AccountManager;
import net.hvacapp.cmvrfforandroid.manager.ProjectManager;
import net.hvacapp.cmvrfforandroid.model.project.Project;
import net.hvacapp.cmvrfforandroid.ui.BaseActivity;
import net.hvacapp.cmvrfforandroid.ui.MainTabBarActivity;
import net.hvacapp.cmvrfforandroid.ui.project.model.GetCityGuidResp;
import net.hvacapp.cmvrfforandroid.ui.selections.model.AreaData;
import net.hvacapp.cmvrfforandroid.utils.AnnotationUtil;
import net.hvacapp.cmvrfforandroid.utils.UIHelper;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@EActivity(R.layout.activity_browser)
public class BrowserActivity extends BaseActivity {

    @ViewById(R.id.webview)
    WebView mWebView;

    private String mUrl;
    private Dialog mDialog;
    private String mCityName;

    /**
     * 页面加载完后执行
     */
    @AfterViews
    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();
    }

    @Override
    public void initBoot() {
        setupToolbar(0, "汇总", null);

//        int ids[] = {R.id.action_share, R.id.action_close};
//        int icons[] = {R.drawable.button_share, R.drawable.button_close};
//        setupToolbarMenuItem(ids, icons, new String[]{"分享", "关闭"}, null);
        setButton2Info("分享", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StringUtils.isEmpty(mCityName)) {
                    mCityName = "未知城市";
                }
                String detail = mCityName + "\n " + ProjectManager.getInstance().getProjectData().getHouse() + "\n " + ProjectManager.getInstance().getProjectData().getArea() + "平米";

                UIHelper.shareFunctionWithUI(BrowserActivity.this, "看看我的项目", detail, mUrl, getScreenBitmap());
            }
        });
        setButton1Info("关闭", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(AnnotationUtil.get(MainTabBarActivity.class));
            }
        });
        getCityByGuid(ProjectManager.getInstance().getProjectData().getGuidId());
    }

    @SuppressLint({"JavascriptInterface", "SetJavaScriptEnabled"})
    @Override
    public void initData(Intent intent) {
        // TODO Auto-generated method stub
        mWebView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);

        WebSettings webSettings = mWebView.getSettings();
        webSettings.setDefaultTextEncodingName("UTF-8");
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAppCacheEnabled(false);

        webSettings.setUseWideViewPort(true);//设定支持viewport
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setSupportZoom(true);//设定支持缩放

        mUrl = HttpConstant.HTTP_GET_PROJECT_HTMLREPORT +
                "?tk=" + AccountManager.getInstance().getToken() +
                "&pid=" + ProjectManager.getInstance().getProjectData().getGuidId();

        // 让页面直接显示在webView客户端，用户不用再去选择浏览器显示。
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url); // 在当前的webview中跳转到新的url
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                mDialog.dismiss();
            }

        });
        mDialog = createProgressBarDialog(BrowserActivity.this,
                "加载中...");
        if (mDialog != null) {
            mDialog.show();
        }
        mWebView.loadUrl(mUrl);
    }

    /***
     * 通过城市ID获得城市
     * @param guidId
     */
    private void getCityByGuid(String guidId) {
        Map<String, Object> param = new HashMap<>();
        param.put("cid", guidId);

        BasicHttpClient.httpGetAsync(HttpConstant.HTTP_GET_CITY, param, new HttpCallbackHandle<GetCityGuidResp>() {
            @Override
            public void onSuccess(GetCityGuidResp obj) {
                // 成功
                mCityName = obj.getGetCityIdResult();
            }

            @Override
            public void onFailure(String response) {

            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_share:
                if (StringUtils.isEmpty(mCityName)) {
                    mCityName = "未知城市";
                }
                String detail = mCityName + "\n " + ProjectManager.getInstance().getProjectData().getHouse() + "\n " + ProjectManager.getInstance().getProjectData().getArea() + "平米";

                UIHelper.shareFunctionWithUI(BrowserActivity.this, "看看我的项目", detail, mUrl, getScreenBitmap());
                break;
            case R.id.action_close:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 页面名称
     */
    @Override
    public String getName() {
        return "";
    }


}
